package com.raspberry.nethelper;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.RectF;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.gcssloop.widget.RockerView;
import com.github.niqdev.mjpeg.DisplayMode;
import com.github.niqdev.mjpeg.Mjpeg;
import com.github.niqdev.mjpeg.MjpegSurfaceView;
import com.raspberry.nethelper.net.SocketHelper;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class StartActivity extends Activity {
    SocketHelper socketHelper;
    int TIMEOUT = 5;

    private String ip = "192.168.31.203";
    /**
     * 摇杆
     */
    private RockerView rocker;
    private MjpegSurfaceView mjpegView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);
        socketHelper = SocketHelper.getInstance(ip, 10086);
        rocker = findViewById(R.id.rocker);
        mjpegView = findViewById(R.id.mjpegView);

        Mjpeg.newInstance()
//                .credential("USERNAME", "PASSWORD")
                .open("http://192.168.31.203:8080/?action=stream", TIMEOUT)
                .subscribe(inputStream -> {
                    mjpegView.setSource(inputStream);
                    mjpegView.setDisplayMode(DisplayMode.BEST_FIT);
                    mjpegView.showFps(true);
                }, throwable -> {
                    Log.e(getClass().getSimpleName(), "mjpeg error", throwable);
                    Toast.makeText(this, "Error", Toast.LENGTH_LONG).show();
                });
        rocker.setListener(new RockerView.RockerListener() {
            @Override
            public void callback(int eventType, int currentAngle, float currentDistance) {
                switch (eventType) {
                    case RockerView.EVENT_ACTION:
                        // 触摸事件回调
                        Log.i("EVENT_ACTION-------->", "angle=" + currentAngle + " - distance" + currentDistance);
                        if ((currentAngle >= 0 && currentAngle < 45) || (currentAngle < 360 && currentAngle >= 315)) {
                            socketHelper.writeString("d");
                            System.out.println("right");
                        } else if (currentAngle >= 45 && currentAngle < 135) {
                            socketHelper.writeString("w");
                            System.out.println("faw");
                        } else if (currentAngle >= 135 && currentAngle < 225) {
                            socketHelper.writeString("a");
                            System.out.println("left");
                        } else if (currentAngle >= 225 && currentAngle < 315) {
                            socketHelper.writeString("s");
                            System.out.println("back");
                        } else {
//                            carUtil.stop();
                            socketHelper.writeString("t");
                            System.out.println("stop");
                        }

                        break;
                    case RockerView.EVENT_CLOCK:
                        // 定时回调
//                        Log.i("EVENT_CLOCK", "angle=" + currentAngle + " - distance" + currentDistance);
                        break;
                }
            }
        });
    }


}
